package org.dromara.Nyt7RegionManagement.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7RegionManagement.domain.bo.Nyt7RegionManagementBo;
import org.dromara.Nyt7RegionManagement.domain.vo.Nyt7RegionManagementVo;
import org.dromara.Nyt7RegionManagement.domain.Nyt7RegionManagement;
import org.dromara.Nyt7RegionManagement.mapper.Nyt7RegionManagementMapper;
import org.dromara.Nyt7RegionManagement.service.INyt7RegionManagementService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 区域管理Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-30
 */
@RequiredArgsConstructor
@Service
public class Nyt7RegionManagementServiceImpl implements INyt7RegionManagementService {

    private final Nyt7RegionManagementMapper baseMapper;

    /**
     * 查询区域管理
     *
     * @param regionId 主键
     * @return 区域管理
     */
    @Override
    public Nyt7RegionManagementVo queryById(Long regionId){
        return baseMapper.selectVoById(regionId);
    }


    /**
     * 查询符合条件的区域管理列表
     *
     * @param bo 查询条件
     * @return 区域管理列表
     */
    @Override
    public List<Nyt7RegionManagementVo> queryList(Nyt7RegionManagementBo bo) {
        LambdaQueryWrapper<Nyt7RegionManagement> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Nyt7RegionManagement> buildQueryWrapper(Nyt7RegionManagementBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7RegionManagement> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPid() != null, Nyt7RegionManagement::getPid, bo.getPid());
        lqw.like(StringUtils.isNotBlank(bo.getName()), Nyt7RegionManagement::getName, bo.getName());
        lqw.eq(bo.getSort() != null, Nyt7RegionManagement::getSort, bo.getSort());
        lqw.eq(bo.getLevel() != null, Nyt7RegionManagement::getLevel, bo.getLevel());
        return lqw;
    }

    /**
     * 新增区域管理
     *
     * @param bo 区域管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7RegionManagementBo bo) {
        Nyt7RegionManagement add = MapstructUtils.convert(bo, Nyt7RegionManagement.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setId(add.getId());
//        }
        return flag;
    }

    /**
     * 修改区域管理
     *
     * @param bo 区域管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7RegionManagementBo bo) {
        Nyt7RegionManagement update = MapstructUtils.convert(bo, Nyt7RegionManagement.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7RegionManagement entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除区域管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
